module.exports = function ($scope, $resource, toaster, Excel, $timeout) {

    Date.prototype.format = function (format) {
        var date = {
            "M+": this.getMonth() + 1,
            "d+": this.getDate(),
            "h+": this.getHours(),
            "m+": this.getMinutes(),
            "s+": this.getSeconds(),
            "q+": Math.floor((this.getMonth() + 3) / 3),
            "S+": this.getMilliseconds()
        };
        if (/(y+)/i.test(format)) {
            format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
        }
        for (var k in date) {
            if (new RegExp("(" + k + ")").test(format)) {
                format = format.replace(RegExp.$1, RegExp.$1.length == 1
                    ? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
            }
        }
        return format;
    }

    function date2str(d) {
        if (d === undefined) {
            return "";
        }
        var month = (d.getMonth() + 1).toString();
        var day = d.getDate().toString();
        if (month.length == 1) month = '0' + month;
        if (day.length == 1) day = '0' + day;
        return d.getFullYear() + "-" + month + "-" + day;
    }
    $scope.start_date = {
        // 'value': date2str(new Date()),
        'value': '',
        'opened': false
    }
    $scope.dateOpen = function ($event, item) {
        $event.preventDefault();
        $event.stopPropagation();
        item.opened = true;
    };
    $scope.end_date = {
        // 'value': date2str(new Date()),
        'value': '',
        'opened': false
    }
    $scope.dateOpen1 = function ($event, item) {
        $event.preventDefault();
        $event.stopPropagation();
        item.opened = true;
    };

    $scope.myKeyup = function (e) {
		//IE 编码包含在window.event.keyCode中，Firefox或Safari 包含在event.which中
		var keycode = window.event ? e.keyCode : e.which;
		if (keycode == 13) {
			$scope.load();
		}
	};

    $scope.paramsMap = {};
    $scope.currentPage = 1;		//当前页码
    $scope.itemsPerPage = 10;	//每页显示几条
    $scope.load = function () {
        let params = {
            'pageNo': $scope.currentPage,
            'pageSize': $scope.itemsPerPage
        }
        if ($scope.start_date.value) {
            if (typeof $scope.start_date.value === 'string') {
                $scope.paramsMap.start_time = $scope.start_date.value
            } else {
                $scope.paramsMap.start_time = date2str($scope.start_date.value);
            }
        } else {
            $scope.paramsMap.start_time = ''
        }
        if ($scope.end_date.value) {
            if (typeof $scope.end_date.value === 'string') {
                $scope.paramsMap.end_time = $scope.end_date.value
            } else {
                $scope.paramsMap.end_time = date2str($scope.end_date.value);
            }
        } else {
            $scope.paramsMap.end_time = ''
        }
        Object.assign(params, $scope.paramsMap)
        $resource('/api/as/puc/subsidynoallow/getNewUserList', {}, {}).save(params, function (res) {
            if (res.errcode === 0) {
                $scope.userList = res.data.results;
                $scope.totalItems = res.data.totalRecord;
            } else {
                toaster.error({ title: '', body: res.errmsg });
            }
        });
    }
    $scope.load();
    
    $scope.updateUserState = function (user) {
        let params = {
            out_id: user.out_id
        }
        $resource('/api/ac/puc/pubSubsidyService/updateUserSubsidyState', {}, {}).save(params, function (res) {
            if (res.errcode === 0) {
                toaster.success({ title: '', body: '修改成功' });
                user.state = '2';
            } else {
                toaster.error({ title: '', body: res.errmsg });
            }
        });
    }

    $scope.export = function (tableId) {
        $resource('/api/as/puc/subsidynoallow/getAllUserList', {}, {}).save({}, function (res) {
            if (res.errcode === 0) {
                $scope.userAllList = res.data;
                $timeout(function () {
                    document.getElementById("dlink").href = Excel.tableToExcel(tableId, 'sheet1');
                    document.getElementById("dlink").click();
                }, 100);
            } else {
                toaster.error({ title: '', body: res.errmsg });
            }
        });
    }

};